package com.dominos.voldemortstress;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;

public class KickOffSingleStressTest {

	/**
	 * @param args
	 * @throws InterruptedException
	 * @throws NullPointerException
	 * @throws MalformedObjectNameException
	 * @throws NotCompliantMBeanException
	 * @throws MBeanRegistrationException
	 * @throws InstanceAlreadyExistsException
	 * @throws IOException
	 */
	public static void main(String[] args) throws InterruptedException,
			MalformedObjectNameException, NullPointerException,
			InstanceAlreadyExistsException, MBeanRegistrationException,
			NotCompliantMBeanException, IOException {
		Logger logger = Logger.getLogger("STRESS");

		/*
		 * I want to know the combination of MaxQueuedRequests, MaxThreads, and
		 * MaxConnectionsPerNode that allow me to launch the greatest number of
		 * threads before I start seeing errors giving 250K of data.
		 */

		String bootstrapUrl = "tcp://localhost:6666";
		StressClientConfig clientConfig = new StressClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		clientConfig.setMaxConnectionsPerNode(25);
		clientConfig.setMaxThreads(1);
		clientConfig.setMaxQueuedRequests(1);

		logger.info("Max Connections Per Node: "
				+ clientConfig.getMaxConnectionsPerNode());
		logger.info("Max Threads: " + clientConfig.getMaxThreads());
		logger.info("Max Queued Requests: "
				+ clientConfig.getMaxQueuedRequests());

		// clientConfig.setEnableJmx(false);
		// clientConfig.setMaxBootstrapRetries(1);
		// clientConfig.setMaxTotalConnections(2);
		// clientConfig.setRoutingTimeout(200, TimeUnit.MILLISECONDS);
		// clientConfig.setSocketBufferSize(50000);
		// clientConfig.setSocketKeepAlive(false);
		// clientConfig.setSocketTimeout(100, TimeUnit.MILLISECONDS);
		// clientConfig.setThreadIdleTime(10, TimeUnit.MILLISECONDS);
		logger.info("Routing Timeout"
				+ clientConfig.getRoutingTimeout(TimeUnit.MILLISECONDS));
		logger.info("Socket Timeout"
				+ clientConfig.getSocketTimeout(TimeUnit.MILLISECONDS));
		logger.info("Thread Idel Time"
				+ clientConfig.getThreadIdleTime(TimeUnit.MILLISECONDS));

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(25);
		stressParameters.setSizeOfWriteData(250000);
		stressParameters.setNumberofReadThreads(0);
		stressParameters.setNumberOfWarmupWrites(5);
		stressParameters.setStoreName("stressTestStore");
		stressParameters
				.setLogFilePath("LogFileForNoErrors5Through25PutThreads.csv");

		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);

		voldemortStress.connectToStore();
		System.out.println("Connected to the store: "
				+ stressParameters.getStoreName());

		voldemortStress.runStressTestForXSeconds(5);

		System.out.println("Total Number of writes: "
				+ voldemortStress.getTotalNumberOfWrites());
		System.out.println("Total Number of writes Errors: "
				+ voldemortStress.getTotalNumberOfWriteErrors());
		System.out.println("Average Write Time: "
				+ voldemortStress.getAverageWriteTime());
		System.out.println("Finished");

		voldemortStress.disconnectFromStore();
		System.exit(0);
	}

}
